package c_api

import (
	"gintest/models"
	"gintest/until"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)
//接收数据
type OrderRequest struct{
	Id int `json:"id"`
	StoreId int `json:"store_id"`
	Status int `json:"status"` 
	Page uint64 `json:"page"` 
	PageSize uint64 `json:"pagesize"` 
}
func Order_getlist(c *gin.Context){
	var data OrderRequest
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	token:=c.Keys["token"].(until.Token)
	//页面
	if data.Page<=0{
		data.Page=1
	}
	if data.PageSize<=0{
		data.PageSize=12
	}
	//商户ID
	if data.StoreId<=0{
		until.Fail(c,1,"商户号不能空")
		return
	}
	if data.Status>7 || data.Status<0{
		until.Fail(c,1,"订单状态错误")
		return
	}
	list:=models.GetOrderListOfStore(data.Page,data.PageSize,data.StoreId,data.Status,token.Id)
	until.Success(c,list)
}

func Order_getinfo(c *gin.Context){
	var data OrderRequest
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	if data.Id<=0{
		until.Fail(c,1,"订单编号不能空")
		return
	}
	info:=models.GetOrderById(data.Id)
	if info.Id!=0{
		until.Success(c,info)
	}else{
		until.Success(c,nil)
	}
}
type BuyGoodsParamRequest struct{
	Id int `json:"id"`
	Num int `json:"num"`
	SpecId int `json:"spec_id"`
}
type BuyGoodsRequest struct{
	Goods []BuyGoodsParamRequest `json:"goods"` 
	DeliveryId int `json:"delivery_id"` //收货地址ID
}
func Order_create(c *gin.Context){
	var data BuyGoodsRequest
	if err:=c.ShouldBindJSON(&data) ;err!=nil {
		until.Fail(c,1,err.Error())
		return
	}
	//检查参数
	if len(data.Goods)<=0{
		until.Fail(c,1,"商品不能为空")
		return
	}
	//检查收货信息
	if data.DeliveryId<=0{
		until.Fail(c,1,"收货地址不能为空")
		return
	}
	//获取商品
	var order models.Order
	for _,v:=range data.Goods{
		var item models.OrderItem
		//检查参数
		if v.Id<=0{
			until.Fail(c,1,"参数错误")
			return
		}
		if v.Num<=0{
			until.Fail(c,1,"购买数量错误")
			return
		}
		//获取商品信息
		goods_info:=models.GetGoodsById(v.Id)
		if goods_info.Id<=0{
			until.Fail(c,1,"未找到商品")
			return
		}
		if len(goods_info.GoodsSpec)>0 && v.SpecId==0{
			until.Fail(c,1,"未选择规格")
			return
		}
		if goods_info.Status!=2{
			until.Fail(c,1,"商品未上架")
			return
		}
		
		
		if v.SpecId!=0{
			var spec models.GoodsSpec
			for _,v1:=range goods_info.GoodsSpec{
				if v.SpecId==v1.Id{
					spec=v1
					break
				}
			}
			if spec.Id==0{
				until.Fail(c,1,"规格错误")
				return
			}
			if spec.Num<=0{
				until.Fail(c,1,"商品无库存")
				return
			}
			if v.Num>=spec.Num{
				until.Fail(c,1,"购买库存数不足")
				return
			}
			item.Money=spec.Price
			item.Title=goods_info.Title+"/"+strconv.Itoa(spec.Id)+"/"+spec.Title
		}else{
			if goods_info.Num<=0{
				until.Fail(c,1,"商品无库存")
				return
			}
			
			if v.Num>=goods_info.Num{
				until.Fail(c,1,"购买库存数不足")
				return
			}
			item.Money=goods_info.Price
			item.Title=goods_info.Title
		}
		item.ItemId=v.Id
		item.Num=v.Num
		item.Image=goods_info.Thumb
		item.TotalMoney=item.Money*float64(item.Num)
		item.CreateAt=time.Now()
		item.UpdateAt=time.Now()
		order.OrderItem=append(order.OrderItem,item)
		order.Money+=item.TotalMoney
		if order.StoreId==0{
			order.StoreId=goods_info.StoreId
		}
		
	}
	//写入用户ID
	token:=c.Keys["token"].(until.Token)
	order.UserId=token.Id
	//生成订单号
	order.OrderNum=strconv.FormatInt(until.RangeRand(180000000,199999999),10)
	//订单状态
	order.Status=0
	//关联默认收货信息
	delivery:=models.GetDeliveryByIdUserId(data.DeliveryId,token.Id)
	if delivery.Id!=0{
		order.OrderDelivery.Name=delivery.Name
		order.OrderDelivery.Phone=delivery.Phone
		order.OrderDelivery.Address=delivery.Address
		order.OrderDelivery.City=delivery.City
		order.OrderDelivery.Province=delivery.Province
		order.OrderDelivery.Latitude=delivery.Latitude
		order.OrderDelivery.Longitude=delivery.Longitude
		order.OrderDelivery.CreateAt=time.Now()
		order.OrderDelivery.UpdateAt=time.Now()
	}
	err:=models.AddOrder(&order)
	if err!=nil{
		until.Fail(c,1,err.Error())
			return
	}
	until.Success(c,order)
}